package com.xunji.user.config;

import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * swagger配置，将Controller的方法进行可视化的展现，像方法注释，方法参数，方法返回值等都提供了相应的用户界面
 *
 * @author xuqiang_imyours
 * @version 1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Value("${swagger.enable:false}")
    private boolean isEnable;

    @Bean
    public Docket controlApi() {

        //请求头参数开启入口
        ParameterBuilder tokenParam = new ParameterBuilder();
        List<Parameter> params = new ArrayList<>();
        tokenParam.name("Authorization").description("Authorization").modelRef(new ModelRef("string")).parameterType("header").required(false);
        params.add(tokenParam.build());
        /*tokenParam.name("from").description("来源").modelRef(new ModelRef("string")).parameterType("header").required(false);
        params.add(tokenParam.build());*/

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(isEnable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .paths(Predicates.not(PathSelectors.regex("/info.*")))
                .paths(Predicates.not(PathSelectors.regex("/autoconfig.*")))
                .paths(Predicates.not(PathSelectors.regex("/health.*")))
                .paths(Predicates.not(PathSelectors.regex("/metrics.*")))
                .paths(Predicates.not(PathSelectors.regex("/mappings.*")))
                .paths(Predicates.not(PathSelectors.regex("/trace.*")))
                .paths(Predicates.not(PathSelectors.regex("/configprops.*")))
                .paths(Predicates.not(PathSelectors.regex("/beans.*")))
                .paths(Predicates.not(PathSelectors.regex("/env.*")))
                .paths(Predicates.not(PathSelectors.regex("/dump.*")))
                .paths(Predicates.not(PathSelectors.regex("/auditevents.*")))
                .paths(Predicates.not(PathSelectors.regex("/docs.*")))
                .paths(Predicates.not(PathSelectors.regex("/archaius.*")))
                .paths(Predicates.not(PathSelectors.regex("/features.*")))
                .paths(Predicates.not(PathSelectors.regex("/pause.*")))
                .paths(Predicates.not(PathSelectors.regex("/refresh.*")))
                .paths(Predicates.not(PathSelectors.regex("/resume.*")))
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
                .paths(Predicates.not(PathSelectors.regex("/jolokia.*")))
                .paths(Predicates.not(PathSelectors.regex("/loggers.*")))
                .paths(Predicates.not(PathSelectors.regex("/restart.*")))
                .paths(Predicates.not(PathSelectors.regex("/service-registry/instance-status.*")))
                .paths(Predicates.not(PathSelectors.regex("/logfile.*")))
                .paths(Predicates.not(PathSelectors.regex("/sessionView.*")))
                .paths(Predicates.not(PathSelectors.regex("/heapdump.*"))).build().globalOperationParameters(params);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("江苏循迹科技发展有限公司")
                .description("控制中心接口")
                //.termsOfServiceUrl("http://www.baidu.com/")
                //.contact(new Contact("小咸鱼杂货铺", "http://www.baidu.com/", ""))
                .version("1.0.0")
                //.license("为人民服务")
                .build();
    }

}
